//
//  SCNHomeModels.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/3/13.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit
import ObjectMapper

class SCNHomeViewModels: NSObject {
    

    
    func requestIndexInitNew(_ param: [String : String], successBlock:@escaping ((_ dataresult: SCNHomeDataresultModels) -> Void)) -> Void {
        
        SCNRequestManager.postRequest(param, URL: SCNRequestAPI.index().initNew) { (response) in

            let homeModels = Mapper<SCNHomeModels>().map(JSONObject: response.result.value)
            
            if let tmp = homeModels?.dataresult {
                successBlock(tmp)
                
            }
        }
        
        /*
         AlamofireObjectMapper 的使用
         一开始是使用 AlamofireObjectMapper 来转模型
         可是在一些特定考虑下，放弃改用 ObjectMapper
         因为->始终需要 responseObject 方法 来传递 model 类
         这样在创建基类的时候大大的增加了复杂度
         
         */
        /// AlamofireObjectMapper 将 SCNHomeModels 直接映射
        
        //        Alamofire.request(SCNRequestAPI.index().initNew, method: .post, parameters: parameters, encoding: URLEncoding.default).responseObject { (response: DataResponse<SCNHomeModels>) in
        //
        //
        //            if response.result.isSuccess {
        //                let homeModels = response.result.value
        //
        //                if let tmp = homeModels {
        //
        //                    if tmp.status == 1 {
        //                        successBlock(tmp.dataresult!)
        //                    }else {
        //                        printLog(tmp.errmsg)
        //                    }
        //
        //                }
        //
        //            }else {
        //
        //                printLog(response.result.error)
        //
        //                failBlock(response.result.error! as NSError)
        //            }
        
        //            let homeModels = response.result.value
        //            let bannerModels = homeModels?.dataresult?.banner
        //            let listModels = homeModels?.dataresult?.list
        //
        //            if let tmp = listModels {
        //
        //                printLog(tmp.count)
        //                for list in tmp {
        //                    printLog(list.advance ?? 0.0)
        //                }
        //            }
        //            
        //            if let tmp = bannerModels {
        //                for benner in tmp {
        //                    printLog(benner.detailsUrl ?? "nil")
        //                }
        //            }
        
        
        
    }
}



/// 必须实现 Mappable 协议
/*
 基于 Swift 安全性语言
 Models 在映射的时候需要一层层去解 
 SCNHomeModels -> SCNHomeDataresultModels -> SCNHomeMarkerModels/SCNHomeBannerModels
 */
class SCNHomeModels: Mappable {
    
    var dataresult: SCNHomeDataresultModels?
    var errmsg: String?
    var status: Int?
    var servertime: Int?
    
    
    
    
    init(){}
    
    required init?(map: Map) {
        mapping(map:map)
    }
    
    func mapping(map: Map) {
        
        dataresult <- map["dataresult"]
        errmsg <- map["errmsg"]
        status <- map["status"]
        servertime <- map["servertime"]
    }
}

class SCNHomeDataresultModels: Mappable {
   
    ///总投资
    var tender_sum: String?
    ///总收益
    var rate_sum: String?
    /// 标集合
    var list: [SCNHomeMarkerModels]?
    /// 图片集合
    var banner: [SCNHomeBannerModels]?
    
    init(){}
    
    required init?(map: Map) {
        mapping(map:map)
    }
    
    func mapping(map: Map) {

        tender_sum <- map["tender_sum"]
        rate_sum <- map["rate_sum"]
        list <- map["list"]
        banner <- map["banners"]
        
    }
}

class SCNHomeMarkerModels: Mappable {
    
    /// 标题
    var title: String?
    ///进度
    var advance: Double?
    ///新手状态
    var tagID: Float?
    ///期限
    var loandue: String?
    ///起投
    var mintendermoney: String?
    ///可投
    var loanmoney: String?
    ///已投
    var tendermoney: String?
    ///总收益率
    var loanrate: String?
    ///加息前收益率
    var originrate: String?
    ///加息
    var extrarate: String?
    ///标的SN编号
    var loansn: Float?
    ///标的状态
    var loanstatus: Float?
    ///是否为活动标
    var isActivity: Bool?
    
    
    
    init(){}
    
    required init?(map: Map) {
        mapping(map:map)
    }
    
    func mapping(map: Map) {
        
        title <- map["title"]
        advance <- map["advance"]
        tagID <- map["typeid"]
        loandue <- map["loandue"]
        mintendermoney <- map["mintendermoney"]
        loanmoney <- map["loanmoney"]
        tendermoney <- map["tendermoney"]
        loanrate <- map["loanrate"]
        originrate <- map["originrate"]
        extrarate <- map["extrarate"]
        loansn <- map["loansn"]
        loanstatus <- map["loanstatus"]
        isActivity <- map["is_activity"]

    }
}

class SCNHomeBannerModels: Mappable {
    
    /// 详情URL
    var detailsUrl: String?
    /// 图片URL
    var imageUrl: String?

    init(){}
    
    required init?(map: Map) {
        mapping(map:map)
    }
    
    func mapping(map: Map) {
        
        detailsUrl <- map["href"]
        imageUrl <- map["image"]

    }
    
}
